package com.boot2.core.conf.mqtt;

import com.boot2.core.utils.StringUtils;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * @author zhangweilin
 * @date 2018/9/20 14:40
 * @description:  mqtt outbound(发送mqtt消息)配置的条件
 */
public class MqttOutboundConditional implements Condition {


    /**
     * 只有配置了outBound信息，才启用outBound配置，否则不需要的工程就不需要连接mqtt了
     * @param conditionContext
     * @param annotatedTypeMetadata
     * @return
     */
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        Environment environment = conditionContext.getEnvironment();
        String mqttUrl = environment.getProperty("mqtt.url");
        if (StringUtils.isNotEmpty(mqttUrl)) {
            String mqttOutboundTopics = environment.getProperty("mqtt.outbound.defaultTopic");
            System.out.println("mqttOutboundTopics = " + mqttOutboundTopics);
            if (StringUtils.isNotEmpty(mqttOutboundTopics)) {
                return true;
            }
        }
        return false;
    }
}